package com.hx.jvm.jvm.tool;

/**
 * 模拟死锁
 *
 * @author jxrjc
 */
public class DeadLock {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            Thread t1 = new Thread(new SynAddRunnable(1, 2), "ThreadA");
            Thread t2 = new Thread(new SynAddRunnable(2, 1), "ThreadB");
            t1.start();
            t2.start();
        }
    }

    static class SynAddRunnable implements Runnable {

        int a, b;

        public SynAddRunnable(int a, int b) {
            super();
            this.a = a;
            this.b = b;
        }

        @Override
        public void run() {
            // A线程需要1的锁  但被B线程占有
            synchronized (Integer.valueOf(a)) {
                // A线程需要2的锁  但被B线程占有
                synchronized (Integer.valueOf(b)) {
                    System.out.println(Thread.currentThread().getName() + ":a+b=" + (a + b));
                }
            }
        }

    }

}
